﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseManager.events;

namespace DatabaseManager
{
    public partial class Form1 : Form
    {
        Database db;
        List<EventHolder> events;
        List<EventRow> eventrows;
        List<EventArgument> eventrowargs;
        List<EventType> eventtypes;
        EventHolder selectedEvent;
        EventRow selectedrow;
        EventArgument selectedArgument;
        int maxrownum = 0;
        int maxargnum = 0;

        public Form1()
        {
            InitializeComponent();
            db = new Database();
            events = new List<EventHolder>();
            eventrows = new List<EventRow>();
            eventrowargs = new List<EventArgument>();
            eventtypes = db.GetAllEventTypes();
            listviewAdd();
            listView1_Refresh();
        }

        public void listviewAdd()
        {
            String[] columns = new String[] { "SELECT", "id", "eventname", "levelid", "beschrijving" };

            foreach (String column in columns)
            {
                listView1.Columns.Add(column);
            }

            String[] columns2 = new String[] { "SELECT", "numrow", "type", "beschrijving" };

            foreach (String column in columns2)
            {
                listView2.Columns.Add(column);
            }

            String[] columns3 = new String[] { "SELECT", "argnum", "argument", "type"};

            foreach (String column in columns3)
            {
                listView3.Columns.Add(column);
            }

            foreach (EventType eventtype in eventtypes)
            {
                comboBox1.Items.Add(eventtype.type);
            }
        }

        private void listView1_Refresh()
        {
            listView1.Items.Clear();
            listView2.Items.Clear();
            listView3.Items.Clear();

            events = db.GetAllEvents();

            foreach (EventHolder eventholder in events)
            {
                listView1.Items.Add("->").SubItems.AddRange(new String[] { "" + eventholder.eventId, "" + eventholder.name, "" + eventholder.levelId, "" + eventholder.description });
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView2.Items.Clear();
            listView3.Items.Clear();

            if (listView1.SelectedItems.Count > 0)
            {
                eventrows = db.GetAllEventRows(events[listView1.SelectedItems[0].Index].eventId);

                foreach (EventRow eventrow in eventrows)
                {
                    if (eventrow.numRow > maxrownum)
                    {
                        maxrownum = eventrow.numRow;
                    }
                    listView2.Items.Add("->").SubItems.AddRange(new String[] { "" + eventrow.numRow, "" + eventrow.type.type, eventrow.type.description });
                }

                selectedEvent = events[(listView1.SelectedItems[0].Index)];

                numericUpDown3.Value = selectedEvent.eventId;
                numericUpDown5.Value = selectedEvent.levelId;
                textBox3.Text = selectedEvent.name;
                textBox4.Text = selectedEvent.description;
            }
            maxRowNumber.Text = "Max: " + maxrownum;
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            listView3.Items.Clear();
            

            if (listView2.SelectedItems.Count > 0)
            {
                selectedrow = eventrows[(listView2.SelectedItems[0].Index)];

                eventrowargs = db.GetAllEventRowArguments(selectedrow.numRow);
                Console.WriteLine(selectedrow.numRow);

                foreach (EventArgument argument in eventrowargs)
                {
                    if (argument.argnum > maxargnum)
                    {
                        maxargnum = argument.argnum;
                    }
                    listView3.Items.Add("->").SubItems.AddRange(new String[] { "" + argument.argnum, "" + argument.argument, ""+argument.type});
                }

                // TODO determine real id position in array
                comboBox1.SelectedIndex = db.eventTypeClass.GetIndexById(selectedrow.type.id);
                numericUpDown4.Value = selectedrow.numRow;

            }
            maxArgNumber.Text = "Max: " + maxargnum;
        }

        private void listView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView3.SelectedItems.Count > 0)
            {
                selectedArgument = eventrowargs[(listView3.SelectedItems[0].Index)];
                numericUpDown7.Value = selectedArgument.argnum;
                textBox6.Text = selectedArgument.argument;
                textBox5.Text = selectedArgument.type;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            // add event
            EventHolder createdEvent = new EventHolder
            (
                (int)numericUpDown3.Value, 
                (int)numericUpDown5.Value, 
                textBox3.Text, 
                textBox4.Text
            );
            db.AddEvent(createdEvent);
            listView1_Refresh();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // add eventrow
            EventRow createdEventRow = new EventRow
            (
                (int) numericUpDown4.Value,
                selectedEvent.eventId,
                db.eventTypeClass.GetEventTypeByIndex(comboBox1.SelectedIndex)
            );
            db.AddEventRow(createdEventRow);
            listView1_SelectedIndexChanged(this, new EventArgs());
        }

        private void button9_Click(object sender, EventArgs e)
        {
            // add argument
            // TODO controleer of argument 2 klopt en werkt
            EventArgument createdEventArgument = new EventArgument
            (
                (int)numericUpDown7.Value,
                selectedrow.numRow,
                textBox6.Text,
                textBox5.Text
            );
            db.AddArgument(createdEventArgument);
            listView2_SelectedIndexChanged(this, new EventArgs());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            // remove event
            EventHolder deletedEvent = new EventHolder
            (
                (int)numericUpDown3.Value,
                (int)numericUpDown5.Value,
                textBox3.Text,
                textBox4.Text
            );
            db.DeleteEvent(deletedEvent);
            listView1_Refresh();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // remove eventrow
            EventRow deletedEventRow = new EventRow
            (
                (int)numericUpDown4.Value,
                selectedEvent.eventId,
                db.eventTypeClass.GetEventTypeById(comboBox1.SelectedIndex)
            );
            db.DeleteEventRow(deletedEventRow);
            listView1_SelectedIndexChanged(this, new EventArgs());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            // remove argument
            EventArgument deletedEventArgument = new EventArgument
            (
                (int)numericUpDown7.Value,
                selectedrow.numRow,
                textBox6.Text,
                textBox5.Text
            );
            db.DeleteArgument(deletedEventArgument);
            listView2_SelectedIndexChanged(this, new EventArgs());
        }

        private void button12_Click(object sender, EventArgs e)
        {
            // modify event
            EventHolder modifiedEvent = new EventHolder
            (
                (int)numericUpDown3.Value,
                (int)numericUpDown5.Value,
                textBox3.Text,
                textBox4.Text
            );
            db.ModifyEvent(modifiedEvent);
            listView1_Refresh();
        }
    }
}
